package com.jlastu.dlxy_androidapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.Message;

import java.util.List;

public class MessageAdapter extends BaseAdapter {

    private Context context;
    private List<Message> messages;

    public MessageAdapter(Context context, List<Message> messages) {
        this.context = context;
        this.messages = messages;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int position) {
        return messages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Message message = messages.get(position);
        if (message.isSent()) {
            convertView = LayoutInflater.from(context).inflate(R.layout.chat_item_sent, parent, false);
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.chat_item_received, parent, false);
        }
        TextView textViewMessage = convertView.findViewById(R.id.text_message);
        textViewMessage.setText(message.getMessage());
        return convertView;
    }
}